package com.elong.android.specialhouse.utils;

import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextWatcher;
import android.widget.EditText;

import com.elong.android.specialhouse.utils.filter.SensitiveWordFilter;

/**
 * 类型功能说明描述
 *
 * @author cheng.wang
 * @date 2015/9/7 17:00
 */
public class InputFilterUtils {

    private static final String SPECIAL_STR = "[\\-\\(\\)\\*\\s\\.\\|\\/\\+\\?\\[\\]\"\'\\!<>《》‘“、,^~`&$？：@;:·￥#%=（）…_—。，]*";
    private static final String DEFAULT_REGEX ="([0-9]" + SPECIAL_STR + "){7,18}[0-9]";

    private static final String ID_REGEX = "[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X|x)";

    /**
     * @param source
     * @return 返回用*替换之后的字符串，若返回空，则表示不含敏感词
     */
    public static boolean containsSensitiveWords(String source,Context context) {
        // 英文统一转为大写
        String str = source.replace(" ", "").toUpperCase();
        SensitiveWordFilter sensitiveWordFilter = new SensitiveWordFilter(context);
        return sensitiveWordFilter.isContaintSensitiveWord(str,1);
    }

    /**
     * 返回一个只能输入汉字或者英文的过滤器
     *
     * @return
     */
    public static InputFilter getZhAndEnInputFilter() {

        return new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                String regex = "^[a-zA-Z\u4e00-\u9fa5]{1,20}$";
                if (!source.toString().matches(regex)) {
                    return "";
                }
                return source;
            }
        };
    }

    /**
     * 返回一个只能输入数字或者英文的过滤器
     *
     * @return
     */
    public static InputFilter getNumAndEnInputFilter() {

        return new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                String regex = "^[A-Za-z0-9]{1,15}$";
                if (!source.toString().matches(regex)) {
                    return "";
                }
                return source;
            }
        };
    }

    /**
     * 返回一个输入过滤器，允许中英文+数字，不允许其他特殊字符
     *
     * @return
     */
    public static InputFilter getNoSpecialCharInputFilter() {

        return new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                String regex = "^[a-zA-Z0-9\u4e00-\u9fa5]{1,20}$";
                if (!source.toString().matches(regex)) {
                    return "";
                }
                return source;
            }
        };
    }

    /**
     * 返回一个地址输入过滤器，允许中英文+数字+特殊字符
     *
     * @return
     */
    public static InputFilter getAddressInputFilter() {
        //TODO:添加允许的特殊字符
        return new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                String regex = "^[a-zA-Z0-9\u4e00-\u9fa5]{1,20}$";
                if (!source.toString().matches(regex)) {
                    return "";
                }
                return source;
            }
        };
    }

    /**
     * 返回一个表情输入过滤器，不允许输入表情
     *
     * @return
     */
    public static InputFilter getEmojiInputFilter() {
        return new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                String srcStr = source.toString();
                if (containsEmoji(srcStr)) {
                    return "";
                }
                return null;
            }
        };
    }

    /**
     * 检测是否有emoji表情
     *
     * @param source
     * @return
     */
    public static boolean containsEmoji(String source) {
        int len = source.length();
        for (int i = 0; i < len; i++) {
            char codePoint = source.charAt(i);
            if (!isEmojiCharacter(codePoint)) {
                //如果不匹配,则该字符是Emoji表情
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否是Emoji
     * @param codePoint 比较的单个字符
     * @return
     */
    private static boolean isEmojiCharacter(char codePoint) {
        return (codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA) || (codePoint == 0xD)
                || ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))
                || ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF));
    }
    /**
     * 设置EditText输入数字范围
     * @param editText
     * @param max 最大值
     * @param min 最小值
     */
    public static void setNumberRegion(final EditText editText,final int max,final int min){
        editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(start>1){

                    if (max != -1 && min != -1) {
                        float num = Float.parseFloat(s.toString());
                        if (num > max) {
                            s = max+"";
                            editText.setText(s);
                            editText.setSelection(s.length());
                        }
                        else if (num < min) {
                            s = min+"";
                            editText.setText(s);
                        }
                        return;
                    }
                }

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
}